package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import edu.chinasoft.domain.Dept;
import edu.chinasoft.service.DeptService;
import edu.chinasoft.service.DeptServiceImp;

/**
 * Servlet implementation class AddDeptforServlet
 */
public class AddDeptforServlet extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(AddDeptforServlet.class);
	private DeptService deptServiceImp;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddDeptforServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		logger.info("AddDeptforServlet>>>doGet start ...");
		logger.info("AddDeptforServlet>>>doGet end ...");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
logger.info("AddDeptforServlet>>>doGet start ...");
		
		//获取表单提交的数据
		String dept_name = request.getParameter("dept_name");
		String dept_location = request.getParameter("dept_location");
		//封装到javabean
		Dept dept = new Dept();
		dept.setDeptName(dept_name);
		dept.setDeptLocation(dept_location);
		//初始化servelt里关于服务层方法成员变量
		 WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		 DeptService deptService = wac.getBean("deptService", DeptService.class);
		 deptService=deptServiceImp;
		//调用服务层方法
		this.deptServiceImp.insertDept(dept);
		response.sendRedirect(""+request.getContextPath()+"/addsuccess.jsp");
		logger.info("AddDeptforServlet>>>doGet end ...");
		
	}

}
